package server.container;

import server.Servlet;

import java.util.*;

/**
 * @author 123
 * @date 2020/4/22 17:22
 * @description
 */
public class Mapper {


    /*端口号*/
    public static Integer port;

    public static void setPort(Integer port) {
        Mapper.port = port;
    }

    /*host 容器*/
    private static Map<String,Host> hostMap=new HashMap<>();

    public static Servlet getServlet(String hostStr,String contextStr,String wrapperStr){
        return hostMap.get(hostStr).getContextMap().get(hostStr+contextStr).getWrapperMap().get(hostStr+contextStr+wrapperStr).getObject();
    }

    /**
     * 映射host
     * 这里偷懒 String对象当容器来使用
     */
    public static void addHost(String hostName,String hostStr){
        if(!hostMap.containsKey(hostName)){
            Host host = new Host(hostName, hostStr);
            hostMap.put(hostName,host);
        }

    }

    /*映射context 这里偷懒 String对象当容器来使用*/
    public static void addContext(String hostName,String contextStr){
        Host host = hostMap.get(hostName);
        Map<String, Context> contextMap = host.getContextMap();
        String contextKey=hostName+contextStr;
        if(contextMap.containsKey(contextKey)){
            return;
        }
        Context context = new Context(contextKey,contextStr);
        contextMap.put(contextKey,context);
    }


    /*映射servlet */
    public static void addWapper(String hostName, String contextStr, String wapperStr, Servlet servlet){
        Host host = hostMap.get(hostName);
        String contextKey=hostName+contextStr;
        Map<String, Wrapper> wrapperMap = host.getContextMap().get(contextKey).getWrapperMap();

        String wapperKey=hostName+contextStr+wapperStr;
        if(wrapperMap.containsKey(wapperKey)){
            return;
        }
        Wrapper wrapper = new Wrapper(wapperKey,servlet);
        wrapperMap.put(wapperKey,wrapper);
    }




    protected abstract static class MapElement<T> {

        public final String name;
        public final T object;

        public MapElement(String name, T object) {
            this.name = name;
            this.object = object;
        }

        public String getName() {
            return name;
        }

        public T getObject() {
            return object;
        }
    }


    protected static final  class Host extends MapElement{

        //key =host
        private Map<String,Context> contextMap;

        public Host(String name, Object object) {
            super(name, object);
            contextMap=new HashMap<>();
        }

        public Map<String, Context> getContextMap() {
            return contextMap;
        }
    }


    protected static final  class Context extends MapElement{

        //key=host+context
        private Map<String,Wrapper> wrapperMap;

        public Context(String name, Object object) {
            super(name, object);
            wrapperMap=new HashMap<>();
        }

        public Map<String, Wrapper> getWrapperMap() {
            return wrapperMap;
        }
    }


    protected static final  class Wrapper  extends MapElement<Servlet>{

        //name=host+context+wrapper
        public Wrapper(String name, Servlet servlet) {
            super(name, servlet);
        }
    }


    public static void main(String[] args) {


    }

}
